package com.ten_4_4.hiring.site.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ten_4_4.hiring.comm.HiringConstants;
import com.ten_4_4.hiring.comm.PageModel;
import com.ten_4_4.hiring.comm.util.TalentUtil;
import com.ten_4_4.hiring.company.po.PositionPo;
import com.ten_4_4.hiring.company.service.ApplicationService;
import com.ten_4_4.hiring.company.service.PositionService;
import com.ten_4_4.hiring.site.po.Comment;
import com.ten_4_4.hiring.site.service.SiteService;
import com.ten_4_4.hiring.talent.po.ResumePo;
import com.ten_4_4.hiring.talent.service.ResumeService;




public class SiteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private PositionService positionService = new PositionService();
    private ResumeService resumeService = new ResumeService();
	private ApplicationService applicationService = new ApplicationService();
	private SiteService siteService = new SiteService();
	
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String act = request.getParameter("act");
		if("toSiteHomepage".equals(act)) {
			toSiteHomepage(request, response);
		} else if ("toPositionPage".equals(act)) {
			toPositionPage(request, response);
		} else if ("showPositionDetail".equals(act)) {
			showPositionDetail(request, response);
		} else if ("loadingData".equals(act)) {
			loadingData(request, response);
		}
	}


	private void loadingData(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int pageNo = Integer.parseInt(request.getParameter("pageNo"));
		PageModel<Comment> commentPageModel = siteService .loadingDataByPaging(pageNo, HiringConstants.DEFAULT_PAGE_SIZE);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(JSON.toJSONString(commentPageModel.getList()));
	}

	private void showPositionDetail(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String positionIdStr = request.getParameter("positionId");
		if(positionIdStr == null || positionIdStr.matches("\\D+")) {
			System.out.println("SiteServlet类中的showPositionDetail方法报错：positionId错误");
			response.sendError(500);
			return;
		}
		int positionId = Integer.parseInt(positionIdStr);
		PositionPo position = positionService.showPositionDetail(positionId);
		if(TalentUtil.getTalentPoFromSession(request) != null) {
			int talentId = TalentUtil.getTalentPoIdFromSession(request);
			List<ResumePo>  resumeList = resumeService.getAllResumeListByTalent(talentId);
			boolean isDelivery = applicationService.isDelivery(talentId, positionId);
			request.setAttribute("isDelivery", isDelivery);
			request.setAttribute("resumeList", resumeList);
		}
		request.setAttribute("position", position);
		request.setAttribute("currentPage", 2);
		request.getRequestDispatcher("WEB-INF/view/site/site-position-detail-page.jsp").forward(request, response);
	}

	private void toPositionPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String searchText = request.getParameter("searchText");
		String pageNoStr = request.getParameter("pageNo");
		PageModel<PositionPo> positionListPageModel = null;
		if(pageNoStr == null) {
			positionListPageModel = positionService.listPositionPaging(searchText, HiringConstants.DEFAULT_PAGE_NO, HiringConstants.DEFAULT_PAGE_SIZE);
		} else {
			int pageNo = Integer.parseInt(pageNoStr);
			positionListPageModel = positionService.listPositionPaging(searchText, pageNo, HiringConstants.DEFAULT_PAGE_SIZE);
		}
		request.setAttribute("positionListPageModel", positionListPageModel);
		request.setAttribute("searchText", searchText);
		request.setAttribute("currentPage", 2);
		request.getRequestDispatcher("WEB-INF/view/site/site-position-page.jsp").forward(request, response);
	}

	private void toSiteHomepage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageNoStr = request.getParameter("pageNo");
		PageModel<Comment> commentPageModel = null;
		if(pageNoStr == null) {
			commentPageModel = siteService .loadingDataByPaging(HiringConstants.DEFAULT_PAGE_NO, HiringConstants.DEFAULT_PAGE_SIZE);
		} else {
			int pageNo = Integer.parseInt(pageNoStr);
			commentPageModel = siteService .loadingDataByPaging(pageNo, HiringConstants.DEFAULT_PAGE_SIZE);
		}
		request.setAttribute("commentPageModel", commentPageModel);
		request.getRequestDispatcher("WEB-INF/view/site/site-homepage.jsp").forward(request, response);
	}

}
